package controllers

import (
	"cronjob/libs"
	"cronjob/models"
	"strconv"
	"strings"
	"time"
)

// 管理员管理
type AdminController struct {
	BaseController
}

func (this *AdminController) List() {
	this.Data["pageTitle"] = "管理员管理"
	this.display()
}

func (this *AdminController) Table() {
	//获取当前页码
	page, err := this.GetInt("page")
	if err != nil {
		page = 1
	}

	//获取每页显示的数量
	limit, err := this.GetInt("limit")
	if err != nil {
		limit = 30
	}
	this.pageSize = limit

	filters := make([]interface{}, 0)
	// filters = append(filters, "status", 1)

	realName := this.GetString("realName")

	if realName != "" {
		filters = append(filters, "login_name__icontains", realName)
	}

	StatusText := make(map[int]string)
	StatusText[0] = "<font color='red'>禁用</font>"
	StatusText[1] = "正常"

	result, count := models.AdminGetList(page, limit, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["login_name"] = v.LoginName
		row["real_name"] = v.RealName
		row["phone"] = v.Phone
		row["email"] = v.Email
		row["status_text"] = StatusText[v.Status]
		row["status"] = v.Status
		list[k] = row

	}
	this.ajaxList("", MSG_OK, count, list)

}

func (this *AdminController) Edit() {
	this.Data["pageTitle"] = "编辑管理员"
	id, _ := this.GetInt("id")

	ainfo, err := models.AdminGetById(id)

	if err != nil {
		return
	}

	row := make(map[string]interface{})
	row["id"] = ainfo.Id
	row["login_name"] = ainfo.LoginName
	row["real_name"] = ainfo.RealName
	row["phone"] = ainfo.Phone
	row["email"] = ainfo.Email
	row["role_ids"] = ainfo.RoleIds

	this.Data["admin"] = row

	role_ids := strings.Split(ainfo.RoleIds, ",")

	filters := make([]interface{}, 0)
	filters = append(filters, "status", 1)
	result, _ := models.RoleGetList(1, 1000, filters...)
	list := make([]map[string]interface{}, len(result))

	for k, v := range result {
		row := make(map[string]interface{})
		row["checked"] = 0
		row["id"] = v.Id
		row["role_name"] = v.RoleName

		for i := 0; i < len(role_ids); i++ {
			role_id, _ := strconv.Atoi(role_ids[i])
			if role_id == v.Id {
				row["checked"] = 1
			}
		}

		list[k] = row
	}

	this.Data["role"] = list

	this.display()
}

func (this *AdminController) Add() {
	this.Data["pageTitle"] = "新增管理员"

	filters := make([]interface{}, 0)
	filters = append(filters, "status", 1)
	result, _ := models.RoleGetList(1, 1000, filters...)
	list := make([]map[string]interface{}, len(result))

	for k, v := range result {
		row := make(map[string]interface{})
		row["checked"] = 0
		row["id"] = v.Id
		row["role_name"] = v.RoleName

		list[k] = row
	}

	this.Data["role"] = list

	this.display()
}

//设置禁用  和启用

func (this *AdminController) AjaxDel() {

	id, _ := this.GetInt("id")

	if id == 1 {
		this.ajaxMsg("超级管理员不可操作", MSG_ERR)
	}

	status := this.GetString("status")

	ainfo, err := models.AdminGetById(id)
	if err != nil {
		this.ajaxMsg("管理员不存在", MSG_ERR)
	}

	Admin_status := 0

	if status == "enable" {
		Admin_status = 1
	}

	ainfo.Status = Admin_status
	ainfo.UpdateTime = time.Now().Unix()
	ainfo.UpdateId = this.userID

	if err := ainfo.Update(); err != nil {
		this.ajaxMsg(err.Error(), MSG_ERR)
	}

	this.ajaxMsg("修改成功", MSG_OK)
}

// 新增。修改
func (this *AdminController) AjaxSave() {
	adminId, _ := this.GetInt("id")

	if adminId == 0 {
		admin := new(models.Admin)

		admin.LoginName = strings.TrimSpace(this.GetString("login_name"))
		admin.RealName = strings.TrimSpace(this.GetString("real_name"))
		admin.Phone = strings.TrimSpace(this.GetString("phone"))
		admin.Email = strings.TrimSpace(this.GetString("email"))
		admin.RoleIds = strings.TrimSpace(this.GetString("roleids"))
		admin.UpdateTime = time.Now().Unix()
		admin.UpdateId = this.userID
		admin.Status = 1

		//判断登录是否存在
		_, err := models.AdminGetByName(admin.LoginName)
		if err != nil {
			this.ajaxMsg("登录名存在", MSG_ERR)
		}

		pwd, salt := libs.Password(4, "")
		admin.Password = pwd
		admin.Salt = salt
		admin.CreateTime = time.Now().Unix()
		admin.CreateId = this.userID
		if _, err := models.AdminAdd(admin); err != nil {
			this.ajaxMsg("", MSG_ERR)
		}
		this.ajaxMsg("新增成功", MSG_OK)
	}

	admin, _ := models.AdminGetById(adminId)

	admin.LoginName = strings.TrimSpace(this.GetString("login_name"))
	admin.RealName = strings.TrimSpace(this.GetString("real_name"))
	admin.Phone = strings.TrimSpace(this.GetString("phone"))
	admin.Email = strings.TrimSpace(this.GetString("email"))
	admin.RoleIds = strings.TrimSpace(this.GetString("roleids"))
	admin.UpdateTime = time.Now().Unix()
	admin.UpdateId = this.userID
	admin.Status = 1

	resetPwd, _ := this.GetInt("reset_pwd")
	if resetPwd == 1 {
		pwd, salt := libs.Password(4, "")
		admin.Password = pwd
		admin.Salt = salt
	}

	if admin.Id == 1 && this.userID != 1 {
		this.ajaxMsg("不可修改超级管理员资料", MSG_ERR)
	}

	if err := admin.Update(); err != nil {
		this.ajaxMsg(err.Error(), MSG_ERR)
	}
	this.ajaxMsg("修改成功", MSG_OK)
}
